/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import boundless.notifications.TradeRequestNotification;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.Dependent;

import boundless.entities.*;
import boundless.ejb.*;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.inject.Inject;


/**
 *
 * @author hendrikdekock
 */
@Named(value = "tradeBean")
@Dependent
public class TradeBean {
   
    
    @Inject
    Fantasyteams myTeam;
    
    Fantasyteams otherTeam;
    
    @EJB
    RequestBean rb;
   
    List<Playerteam> myBenchedPlayers;
    List<Playerteam> otherTeamPlayers;
    
    List<TradeRequestNotification> outstandingRequests;
    TradeRequestNotification outstandingRequest;
    
    String error;
    
    /**
     * Creates a new instance of TradeBean
     */
    public TradeBean() {
        outstandingRequests = new ArrayList<TradeRequestNotification>();
    }
    
    /**
     * Updates the list of outstanding requests
     */
    public void viewOutstandingRequests() {
        
        List<Traderequests> requests = rb.getTradeRequestsForUser(myTeam);
        for (Traderequests t: requests) {
            outstandingRequests.add(new TradeRequestNotification(t, rb));
        }
    }
    
    /**
     * Updates the request being looked at
     * @param i : Where in the list the outstanding request is
     */
    public void viewTradeNotification(int i) {
        
        outstandingRequest = outstandingRequests.get(i);
        
    }
    
    public void setRequestBean(RequestBean rb) {
        this.rb = rb;
    }
    
    public void setMyTeam(Fantasyteams ft) {
        myTeam = ft;
        myBenchedPlayers = rb.getBenchedPlayersOfTeam(ft);
    }
    
    /**
     * Updates the list of benched players of the user's own team.
     */
    public void updateBenchedPlayers() {
        myBenchedPlayers = rb.getBenchedPlayersOfTeam(myTeam);
    }
    
    
    public void setOtherTeam(Fantasyteams ft) {
        otherTeam = ft;
        otherTeamPlayers = rb.getBenchedPlayersOfTeam(ft);
    }
    
    public void searchOtherTeam(String name) {
        Fantasyteams ft = rb.getTeamByName(name, myTeam.getLeagueid());
        otherTeam = ft;
        otherTeamPlayers = rb.getBenchedPlayersOfTeam(ft);   
    }
    
    public void proposeTrade(Playerteam mine, Playerteam theirs) {
        rb.makeTradeRequest(mine.getTeam(), theirs.getTeam(), mine.getPlayer(), theirs.getPlayer());
    }
    
}
